home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / syscalls / read / readTest.c < prev   
Encoding:
C/C++ Source or Header  |  1990-05-31  |  3.3 KB  |  186 lines

  1. /* 
  2.  * fileTest.c --
  3.  *
  4.  *    Test the read, write, open, close and lseek system call.
  5.  *
  6.  * Copyright 1990 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header$";
  18. #endif /* not lint */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/wait.h>
  25.  
  26. extern int errno;
  27. static int errors;
  28.  
  29.  
  30. /*
  31.  *----------------------------------------------------------------------
  32.  *
  33.  * main --
  34.  *
  35.  *
  36.  * Results:
  37.  *    Exits with a zero exit status if everything works properly,
  38.  *      non-zero otherwise.
  39.  *
  40.  * Side effects:
  41.  *
  42.  *----------------------------------------------------------------------
  43.  */
  44.  
  45. void
  46. main(void)
  47. {
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.     exit((errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
  56. }
  57.  
  58.  
  59.  
  60. static void
  61. testClose()
  62. {
  63.     int fd;
  64.  
  65.  
  66.     fd = open(tempfile) ...
  67.  
  68.  
  69.     write(fd ....)
  70.     if (lseek(.... )) ...
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.     if (close(fd)) {
  79.     (void) fprintf(stderr, "close of %s failed: %s\n",
  80.         tempfile, strerror(errno));
  81.     ++errors;
  82.     return;
  83.     }
  84.     if (close(fd) != -1) {
  85.     (void) fprintf(stderr,
  86.         "Close of already closed descriptor succeeded\n");
  87.     ++errors;
  88.     return;
  89.     } else if (errno != EBADF) {
  90.         (void) fprintf(stderr,
  91.         "Bad errno (%d): %s\n", errno, strerror(errno));
  92.         ++errors;
  93.         return;
  94.     }
  95.     }
  96.     if (write(fd, buffer, sizeof(buffer)) != -1) {
  97.     (void) fprintf(stderr, "Write to closed file succeeded.\n");
  98.     ++errors;
  99.     return;
  100.     } else if (errno != EBADF) {
  101.         (void) fprintf(stderr,
  102.         "Bad errno (%d): %s\n", errno, strerror(errno));
  103.         ++errors;
  104.     }
  105.     }
  106.     if (read(fd, buffer, sizeof(buffer)) != -1) {
  107.     (void) fprintf(stderr, "Read from closed file succeeded.\n");
  108.     ++errors;
  109.     return;
  110.     } else if (errno != EBADF) {
  111.         (void) fprintf(stderr,
  112.         "Bad errno (%d): %s\n", errno, strerror(errno));
  113.         ++errors;
  114.     }
  115.     }
  116.     return;
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. /*
  130.  *----------------------------------------------------------------------
  131.  *
  132.  * testExit --
  133.  *
  134.  *    Fork off a child process that exits with the given exit code.
  135.  *      Wait for the child to exit.  Check the return status and make
  136.  *      sure that it matches the exit status.
  137.  *
  138.  * Results:
  139.  *    none.
  140.  *
  141.  * Side effects:
  142.  *      Prints a message to stderr nad increments `errors' if there is
  143.  *      an error.
  144.  *
  145.  *----------------------------------------------------------------------
  146.  */
  147.  
  148. static void
  149. testExit(exitCode)
  150.     int exitCode;
  151. {
  152.     int w;
  153.     int child;
  154.     union wait ws;
  155.  
  156.     switch (child = fork()) {
  157.  
  158.     case 0:
  159.     exit(exitCode);
  160.     (void) fprintf(stderr, "Exit returned: %s\n", strerror(errno));
  161.     abort();
  162.     (void) fprintf(stderr, "Abort returned: %s\n", strerror(errno));
  163.     for (;;) {
  164.         continue;
  165.     }
  166.  
  167.     case -1:
  168.     (void) fprintf(stderr, "Fork failed: %s\n", strerror(errno));
  169.     ++errors;
  170.     break;
  171.  
  172.     default:
  173.     while ((w = wait(&ws)) > 0 && w != child) {
  174.         continue;
  175.     }
  176.     if (ws.w_retcode != exitCode) {
  177.         (void) fprintf(stderr, "Exit returned %d, instead of %d.\n",
  178.         ws.w_retcode, exitCode);
  179.         ++errors;
  180.     }
  181.     break;
  182.     }
  183.     return;
  184. }
  185.  
  186.